home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / k3biso9660.h < prev    next >
Encoding:
C/C++ Source or Header  |  2008-05-27  |  10.9 KB  |  454 lines

  1. /* 
  2.  *
  3.  * $Id: k3biso9660.h 619556 2007-01-03 17:38:12Z trueg $
  4.  * Copyright (C) 2003 Sebastian Trueg <trueg@k3b.org>
  5.  *
  6.  * This file is part of the K3b project.
  7.  * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  * See the file "COPYING" for the exact licensing terms.
  14.  */
  15.  
  16.  
  17. #ifndef _K3B_ISO9660_H_
  18. #define _K3B_ISO9660_H_
  19.  
  20. #include <sys/stat.h>
  21. #include <sys/types.h>
  22.  
  23. #include <qdatetime.h>
  24. #include <qstring.h>
  25. #include <qstringlist.h>
  26. #include <qdict.h>
  27.  
  28. #include "k3b_export.h"
  29.  
  30.  
  31. namespace K3bDevice {
  32.   class Device;
  33. }
  34.  
  35. class K3bIso9660;
  36. class K3bIso9660Backend;
  37. struct iso_directory_record;
  38. struct el_torito_boot_descriptor;
  39. struct iso_primary_descriptor;
  40.  
  41. typedef long sector_t;
  42.  
  43.  
  44.  
  45. /**
  46.  * Simplyfied primary descriptor which just contains the fields
  47.  * used by K3b.
  48.  */
  49. class LIBK3B_EXPORT K3bIso9660SimplePrimaryDescriptor
  50. {
  51.  public:
  52.   /**
  53.    * Creates an empty descriptor
  54.    */
  55.   K3bIso9660SimplePrimaryDescriptor();
  56.  
  57.   QString volumeId;
  58.   QString systemId;
  59.   QString volumeSetId;
  60.   QString publisherId;
  61.   QString preparerId;
  62.   QString applicationId;
  63.   int volumeSetSize;
  64.   int volumeSetNumber;
  65.   long logicalBlockSize;
  66.   long long volumeSpaceSize;
  67. };
  68.  
  69.  
  70. LIBK3B_EXPORT bool operator==( const K3bIso9660SimplePrimaryDescriptor& d1,
  71.                    const K3bIso9660SimplePrimaryDescriptor& d2 );
  72. LIBK3B_EXPORT bool operator!=( const K3bIso9660SimplePrimaryDescriptor& d1,
  73.                    const K3bIso9660SimplePrimaryDescriptor& d2 );
  74.  
  75.  
  76. /**
  77.  * Base class for all entries in a K3bIso9660 archive. A lot has been copied
  78.  * from KArchive.
  79.  */
  80. class LIBK3B_EXPORT K3bIso9660Entry
  81. {
  82.  public:
  83.   K3bIso9660Entry( K3bIso9660* archive,
  84.            const QString& isoName,
  85.            const QString& name,
  86.            int access,
  87.            int date,
  88.            int adate,
  89.            int cdate, 
  90.            const QString& user,
  91.            const QString& group,
  92.            const QString& symlink );
  93.   virtual ~K3bIso9660Entry();
  94.  
  95.   int adate() const { return m_adate; }
  96.   int cdate() const { return m_cdate; }
  97.  
  98.   /**
  99.    * Creation date of the file.
  100.    * @return the creation date
  101.    */
  102.   QDateTime datetime() const;
  103.  
  104.   /**
  105.    * Creation date of the file.
  106.    * @return the creation date in seconds since 1970
  107.    */
  108.   int date() const { return m_date; }
  109.  
  110.   /**
  111.    * Name of the file without path.
  112.    * @return The file name without path.
  113.    */
  114.   const QString& name() const { return m_name; }
  115.  
  116.   /**
  117.    * \return The raw name as saved in the ISO9660 tree
  118.    */
  119.   const QString& isoName() const { return m_isoName; }
  120.  
  121.   /**
  122.    * The permissions and mode flags as returned by the stat() function
  123.    * in st_mode.
  124.    * @return the permissions
  125.    */
  126.   mode_t permissions() const { return m_access; }
  127.  
  128.   /**
  129.    * User who created the file.
  130.    * @return the owner of the file
  131.    */
  132.   const QString& user() const { return m_user; }
  133.  
  134.   /**
  135.    * Group of the user who created the file.
  136.    * @return the group of the file
  137.    */
  138.   const QString& group() const { return m_group; }
  139.  
  140.   /**
  141.    * Symlink if there is one.
  142.    * @return the symlink, or QString::null
  143.    */
  144.   const QString& symlink() const { return m_symlink; }
  145.  
  146.   /**
  147.    * Checks whether the entry is a file.
  148.    * @return true if this entry is a file
  149.    */
  150.   virtual bool isFile() const { return false; }
  151.  
  152.   /**
  153.    * Checks whether the entry is a directory.
  154.    * @return true if this entry is a directory
  155.    */
  156.   virtual bool isDirectory() const { return false; }
  157.  
  158.   K3bIso9660* archive() const { return m_archive; }
  159.  
  160.  private:
  161.   int m_adate;
  162.   int m_cdate;
  163.   QString m_name;
  164.   QString m_isoName;
  165.   int m_date;
  166.   mode_t m_access;
  167.   QString m_user;
  168.   QString m_group;
  169.   QString m_symlink;
  170.   K3bIso9660* m_archive;
  171. };
  172.  
  173.  
  174. class LIBK3B_EXPORT K3bIso9660Directory : public K3bIso9660Entry
  175. {
  176.  public: 
  177.   K3bIso9660Directory( K3bIso9660* archive, 
  178.                const QString& isoName,
  179.                const QString& name, 
  180.                int access, 
  181.                int date,
  182.                int adate,
  183.                int cdate, 
  184.                const QString& user,
  185.                const QString& group,
  186.                const QString& symlink,
  187.                unsigned int pos = 0, 
  188.                unsigned int size = 0 );
  189.   ~K3bIso9660Directory();
  190.  
  191.   /**
  192.    * Returns a list of sub-entries.
  193.    * @return the names of all entries in this directory (filenames, no path).
  194.    */
  195.   QStringList entries() const;
  196.  
  197.   /**
  198.    * Returns the entry with the given name.
  199.    * @param name may be "test1", "mydir/test3", "mydir/mysubdir/test3", etc.
  200.    * @return a pointer to the entry in the directory.
  201.    */
  202.   K3bIso9660Entry* entry( const QString& name );
  203.  
  204.   /**
  205.    * Returns the entry with the given name.
  206.    * @param name may be "test1", "mydir/test3", "mydir/mysubdir/test3", etc.
  207.    * @return a pointer to the entry in the directory.
  208.    */
  209.   const K3bIso9660Entry* entry( const QString& name ) const;
  210.  
  211.   /**
  212.    * Returns a list of sub-entries.
  213.    * Searches for Iso9660 names.
  214.    * @return the names of all entries in this directory (filenames, no path).
  215.    */
  216.   QStringList iso9660Entries() const;
  217.  
  218.   /**
  219.    * Returns the entry with the given name.
  220.    * Searches for Iso9660 names.
  221.    * @param name may be "test1", "mydir/test3", "mydir/mysubdir/test3", etc.
  222.    * @return a pointer to the entry in the directory.
  223.    */
  224.   K3bIso9660Entry* iso9660Entry( const QString& name );
  225.  
  226.   /**
  227.    * Returns the entry with the given name.
  228.    * Searches for Iso9660 names.
  229.    * @param name may be "test1", "mydir/test3", "mydir/mysubdir/test3", etc.
  230.    * @return a pointer to the entry in the directory.
  231.    */
  232.   const K3bIso9660Entry* iso9660Entry( const QString& name ) const;
  233.  
  234.   /**
  235.    * @internal
  236.    * Adds a new entry to the directory.
  237.    */
  238.   void addEntry( K3bIso9660Entry* );
  239.  
  240.   /**
  241.    * Checks whether this entry is a directory.
  242.    * @return true, since this entry is a directory
  243.    */
  244.   bool isDirectory() const { return true; }
  245.  
  246.  private:
  247.   void expand();
  248.  
  249.   QDict<K3bIso9660Entry> m_entries;
  250.   QDict<K3bIso9660Entry> m_iso9660Entries;
  251.  
  252.   bool m_bExpanded;
  253.   unsigned int m_startSector;
  254.   unsigned int m_size;
  255. };
  256.  
  257.  
  258. class LIBK3B_EXPORT K3bIso9660File : public K3bIso9660Entry
  259. {
  260.  public: 
  261.   /**
  262.    * @param pos start sector
  263.    */
  264.   K3bIso9660File( K3bIso9660* archive, 
  265.           const QString& isoName,
  266.           const QString& name, 
  267.           int access, 
  268.           int date,
  269.           int adate,
  270.           int cdate, 
  271.           const QString& user, 
  272.           const QString& group,
  273.           const QString& symlink, 
  274.           unsigned int pos, 
  275.           unsigned int size );
  276.   ~K3bIso9660File();
  277.  
  278.   bool isFile() const { return true; }
  279.  
  280.   void setZF( char algo[2], char parms[2], int realsize );
  281.   int realsize() const { return m_realsize; }
  282.  
  283.   /**
  284.    * @return size in bytes.
  285.    */
  286.   unsigned int size() const { return m_size; }
  287.  
  288.   /**
  289.    * Returnes the startSector of the file.
  290.    */
  291.   unsigned int startSector() const { return m_startSector; }
  292.  
  293.   /**
  294.    * Returnes the startOffset of the file in bytes.
  295.    */
  296.   unsigned long long startPostion() const { return (unsigned long long)m_startSector * 2048; }
  297.  
  298.   /**
  299.    * @param pos offset in bytes
  300.    * @param len max number of bytes to read
  301.    */
  302.   int read( unsigned int pos, char* data, int len ) const;
  303.  
  304.   /**
  305.    * Copy this file to a url.
  306.    */
  307.   bool copyTo( const QString& url ) const;
  308.  
  309.  private:
  310.   char m_algo[2];
  311.   char m_parms[2];
  312.   int m_realsize;
  313.  
  314.   unsigned int m_curpos;
  315.   unsigned int m_startSector;
  316.   unsigned int m_size;
  317. };
  318.  
  319.  
  320. /**
  321.  * This class is based on the KIso class by
  322.  * Gy∩┐╜gy Szombathelyi <gyurco@users.sourceforge.net>.
  323.  * A lot has been changed and bugfixed.
  324.  * The API has been improved to be useful.
  325.  *
  326.  * Due to the stupid Qt which does not support large files as default
  327.  * we cannot use QIODevice with DVDs! That's why we have our own 
  328.  * reading code which is not allowed by KArchive (which is limited to int
  329.  * by the way... who the hell designed this?)
  330.  * I also removed the KArchive inheritance because of the named reasons.
  331.  * So this stuff contains a lot KArchive code which has been made usable.
  332.  *
  333.  * That does not mean that this class is well designed. No, it's not. :)
  334.  *
  335.  * Opening a K3bIso9660 object should be fast since creation of the directory 
  336.  * and file entries is not done until a call to K3bIso9660Directory::entries.
  337. */
  338. class LIBK3B_EXPORT K3bIso9660
  339. {
  340.  public:
  341.   /**
  342.    * Creates an instance that operates on the given filename.
  343.    * using the compression filter associated to given mimetype.
  344.    *
  345.    * @param filename is a local path (e.g. "/home/weis/myfile.tgz")
  346.    */
  347.   K3bIso9660( const QString& filename );
  348.  
  349.   /**
  350.    * Special case which always reads the TOC from the specified sector
  351.    * thus supporting multisession CDs.
  352.    */
  353.   K3bIso9660( K3bDevice::Device* dev, unsigned int startSector = 0 );
  354.  
  355.   /**
  356.    * @param fd open file descriptor
  357.    */
  358.   K3bIso9660( int fd );
  359.  
  360.   /**
  361.    * Directly specify the backend to read from.
  362.    * K3bIso9660 will take ownership of the backend and delete it.
  363.    */
  364.   K3bIso9660( K3bIso9660Backend* );
  365.  
  366.   /**
  367.    * If the .iso is still opened, then it will be
  368.    * closed automatically by the destructor.
  369.    */
  370.   virtual ~K3bIso9660();
  371.  
  372.   /**
  373.    * Set where to start reading in the source.
  374.    */
  375.   void setStartSector( unsigned int startSector );
  376.  
  377.   /**
  378.    * If set to true before opening K3bIso9660 will ignore RR and joliet extensions
  379.    * and only create plain iso9660 names.
  380.    */
  381.   void setPlainIso9660( bool );
  382.  
  383.   bool plainIso9660() const;
  384.  
  385.   /**
  386.    * Opens the archive for reading.
  387.    * Parses the directory listing of the archive
  388.    * and creates the K3bIso9660Directory/K3bIso9660File entries.
  389.    */
  390.   bool open();
  391.  
  392.   bool isOpen() const;
  393.  
  394.   /**
  395.    * Closes everything.
  396.    * This is also called in the destructor
  397.    */
  398.   void close();
  399.  
  400.   /**
  401.    * @param sector startsector
  402.    * @param len number of sectors
  403.    * @return number of sectors read or -1 on error
  404.    */
  405.   int read( unsigned int sector, char* data, int len );
  406.  
  407.   /**
  408.    * The name of the os file, as passed to the constructor
  409.    * Null if you did not use the QString constructor.
  410.    */
  411.   const QString& fileName() { return m_filename; }
  412.  
  413.   const K3bIso9660Directory* firstJolietDirEntry() const;
  414.   const K3bIso9660Directory* firstRRDirEntry() const;
  415.   const K3bIso9660Directory* firstIsoDirEntry() const;
  416.   const K3bIso9660Directory* firstElToritoEntry() const;
  417.  
  418.   /**
  419.    * @returns 0 if no joliet desc could be found
  420.    *          the joliet level (1-3) otherwise
  421.    */
  422.   int jolietLevel() const { return m_joliet; }
  423.  
  424.   const K3bIso9660SimplePrimaryDescriptor& primaryDescriptor() const;
  425.  
  426.   void debug() const;
  427.  
  428.  private:
  429.   /**
  430.    * @internal
  431.    */
  432.   void addBoot( struct el_torito_boot_descriptor* bootdesc );
  433.   void createSimplePrimaryDesc( struct iso_primary_descriptor* desc );
  434.  
  435.   void debugEntry( const K3bIso9660Entry*, int depth ) const;
  436.  
  437.   int m_joliet;
  438.  
  439.   // only used for creation
  440.   static int read_callback( char* buf, sector_t start, int len, void* udata );
  441.   static int isofs_callback( struct iso_directory_record* idr, void *udata );
  442.   K3bIso9660Directory *dirent;
  443.   bool m_rr;
  444.   friend class K3bIso9660Directory;
  445.   
  446.  private:
  447.   QString m_filename;
  448.  
  449.   class Private;
  450.   Private * d;
  451. };
  452.  
  453. #endif
  454.